<?php
namespace jm;

/**
 *  微信登录类
 */
class Wechatlogo
{
            public $APPID="wx4c9125dc430aea97";
            public $SECRET="2f49f4f347baf430e0e9e794c7d076b4";
            public $token="";
              
                // 1、前端获取到 code 后传给后端
            public function getCode($code,$type="h5"){
                        $appid= $this->appid; 
                	 	$url="https://open.weixin.qq.com/connect/oauth2/authorize?";
                		//appid
                		$url.="appid=".$appid;
                		$url.="&redirect_uri=";
                		//  这里是回调的域名和地址
                		$url.="http://".$_SERVER['SERVER_NAME']."/api/Weixinlogin";
                		//下面的都是固定参数
                		$url.="&response_type=code";
                		$url.="&scope=snsapi_userinfo";
                		$url.="&state="."123";
                		$url.="#wechat_redirect"; 
                		$zz=$this->geturl($url);
                        return $zz;die;
                	//	header("location: ".$url) ; exit();
                 
                }
                // 2、通过 code 获取 openid
                public function getOpenid($code,$type="h5"){
                        if($type=="miniprogram"){
                           	    $httpcurl="https://api.weixin.qq.com/sns/jscode2session?"; 
                                 $httpcurl.="appid=".$this->APPID;
                                 $httpcurl.="&secret=".$this->SECRET;
                                 $httpcurl.="&js_code=".$code;
                                $httpcurl.="&grant_type=authorization_code";
                             
                        }elseif ($type=="h5") {
                           	$httpcurl="https://api.weixin.qq.com/sns/oauth2/access_token?";
                           	$httpcurl.="appid=".$this->APPID;
                            $httpcurl.="&secret=".$this->SECRET;
                             $httpcurl.="&code=".$code;
                            $httpcurl.="&grant_type=authorization_code";
                       
                        }elseif($type=="app") {
                           	$httpcurl=" https://api.weixin.qq.com/sns/oauth2/access_token?";  	
                           	$httpcurl.="appid=".$this->APPID;
                            $httpcurl.="&secret=".$this->SECRET;
                            $httpcurl.="&code=".$code;
                            $httpcurl.="&grant_type=authorization_code";
                        }elseif($type=="pc") {   
                           	$httpcurl=" https://api.weixin.qq.com/sns/oauth2/access_token?";  	
                           	$httpcurl.="appid=".$this->APPID;
                            $httpcurl.="&secret=".$this->SECRET;
                            $httpcurl.="&code=".$code;
                            $httpcurl.="&grant_type=authorization_code";
                        }
               
                    $zz=$this->geturl($httpcurl);
                    return $zz;
                }
                //3、通过openid获取用户信息
                public function getUserinfo($access_token,$openid,$type="h5"){
                        //https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
                        if($type=="miniprogram"){
                           	$get_user_info_url="https://api.weixin.qq.com/wxa/getpaidunionid?"; 
                           	$get_user_info_url.='access_token='.$access_token;
                			$get_user_info_url.='&openid='.$openid.'&lang=zh_CN';
                             
                        }elseif ($type=="h5") {
                           	$get_user_info_url="https://api.weixin.qq.com/sns/userinfo?"; 
                           	$get_user_info_url.='access_token='.$access_token;
                			$get_user_info_url.='&openid='.$openid.'&lang=zh_CN';
                        
                        }elseif($type=="app") {
                           	$get_user_info_url="https://api.weixin.qq.com/sns/userinfo?";  	
                           	$get_user_info_url.='access_token='.$access_token;
                			$get_user_info_url.='&openid='.$openid.'&lang=zh_CN';
                        }elseif($type=="pc") {   
                           	$get_user_info_url="https://api.weixin.qq.com/sns/userinfo?";  	
                           	$get_user_info_url.='access_token='.$access_token;
                			$get_user_info_url.='&openid='.$openid.'&lang=zh_CN';
                        }
                        
                			$get_user_info_url='https://api.weixin.qq.com/sns/userinfo';
                		
                		  $data=$this->geturl($get_user_info_url);
                		 
                       return $data;
                }
                
                 
                //getcurl
                public function geturl($url){
                        $headerArray =array("Content-type:application/json;","Accept:application/json");
                        $ch = curl_init();
                        curl_setopt($ch, CURLOPT_URL, $url);
                        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
                        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                        curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray);
                        $output = curl_exec($ch);
                        		//关闭URL请求
                		if($output === FALSE ){
                				echo "CURL Error:".curl_error($ch);
                		} 
                        curl_close($ch);
                        $output = json_decode($output,true);
                        return $output;
                }
                 //1、获取二维码
                  public function QrCode($url ){
                    //   "http://www.taianyuannongmu.com/business/login/weixin2"
                   
                    //扫码后回调地址
                    $url=urlencode($url);
                    
                    $headerurl="https://open.weixin.qq.com/connect/qrconnect?appid=".$this->APPID;
                    $headerurl.="&redirect_uri=".$url;
                    $headerurl.="&response_type=code&scope=snsapi_login&state=wechat_redirect";
                    // header("Location:".$headerurl);
                    return $headerurl;
                  }
            
}

